package cn.otrue.patienthealthmanager.activity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;
import android.widget.Toast;
import cn.otrue.patienthealthmanager.R;
import cn.otrue.patienthealthmanager.adapter.CommunityPostInfoAdpater;
import cn.otrue.patienthealthmanager.bean.ThreadModel;
import cn.otrue.patienthealthmanager.bean.TransferParameter;
import cn.otrue.patienthealthmanager.http.HttpCons;
import cn.otrue.patienthealthmanager.http.HttpServer;
import cn.otrue.patienthealthmanager.http.ServerHandler;
import cn.otrue.patienthealthmanager.view.XListView;
import cn.otrue.patienthealthmanager.view.XListView.IXListViewListener;

import com.google.gson.reflect.TypeToken;

/**
 * 帖子回复
 * 
 * @author G
 * 
 */
public class HuiFUActivity extends BaseActivity implements
		IXListViewListener {

	private List<ThreadModel> goods;
	private CommunityPostInfoAdpater postAdapter;
	private int currentPage = 1;

	private String sectionName;
	private XListView lv_community_partition;



	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_yisheng_pinglun);

		initView();
	}

	private void initView() {
		goods = new ArrayList<ThreadModel>();
		lv_community_partition = (XListView) findViewById(R.id.lv_community_partition);
		lv_community_partition.setPullLoadEnable(false); // 是否开启上拉加载
		lv_community_partition.setXListViewListener(this);// 上啦加载和下拉刷新的监听
	
		getData();
		gethuifu(currentPage);
		
		initTitleBar();
	

	}



	public void initTitleBar() {
		TextView tv_title_title = (TextView) findViewById(R.id.tv_title_title);
		tv_title_title.setText(R.string.compp);
	
		

		TextView tv_title_back = (TextView) findViewById(R.id.tv_title_back);
		tv_title_back.setVisibility(View.VISIBLE);
		tv_title_back.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				finish();
			}
		});

	}




	private Handler faHandler = new ServerHandler() {
		@Override
		public void onSuccess(Bundle data, String state, String msg) {
			onLoad();
			if (state.equals("001")) {
				String tuu = data.getString(HttpCons.DATA);
				List<ThreadModel> iiii = HttpServer.gson.fromJson(tuu,
						new TypeToken<List<ThreadModel>>() {
						}.getType());
				
				goods.addAll(iiii);
				if (goods.size()>5) {
					lv_community_partition.setPullLoadEnable(true);
				}
					postAdapter = new CommunityPostInfoAdpater(goods,
							HuiFUActivity.this);
					lv_community_partition.setAdapter(postAdapter);
					Toast.makeText(HuiFUActivity.this, "操作成功", 0).show();
			
			
		
				
			} else {
				onLoad();
				Toast.makeText(HuiFUActivity.this, msg, 0).show();
			}
		}

		@Override
		public void onFail(Bundle data, String state) {
			onLoad();
			if (state.equals(HttpCons.STATE_EXCEPTION)) {
				// 代表进入状态STATE_EXCEPTION 网络异常
				Toast.makeText(HuiFUActivity.this, "网络异常,请稍后重试",
						Toast.LENGTH_SHORT).show();

			} else if (state.equals(HttpCons.STATE_FORMAT_ERROR)) {
				// 代表进入STATE_FORMAT_ERROR(不是Json)
			} else {
				// 代表进入STATE_ANALYTICAL_ERROR(Json解析错误)

			}
		}
	};

	private int threadId;

	private void getData() {
	threadId = getIntent().getIntExtra("threadId", 0);
	}

	private void onLoad() {
		lv_community_partition.stopRefresh();
		lv_community_partition.stopLoadMore();
		lv_community_partition.setRefreshTime("刚刚");
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
	

		default:
			break;
		}

	}

	private void gethuifu(int i) {
		
		HashMap<String, Object> map = new HashMap<String, Object>();
		map.put("threadId", threadId);
		TransferParameter parameter = new TransferParameter();
		parameter.setUsername("");
		parameter.setPassword(" ");
		parameter.setCurrentPage(i+"");
		parameter.setPagesize("20");
		parameter.setData(map);
		new HttpServer().post(HttpCons.answeredList, parameter, faHandler);
	}

	@Override
	public void onRefresh() {
		// TODO Auto-generated method stub
		currentPage = 1;
		goods.clear();
		gethuifu(currentPage);
		}



	@Override
	public void onLoadMore() {
		// TODO Auto-generated method stub
		currentPage++;
		gethuifu(currentPage);
	
	}

}
